package com.thunder.soft.common.net;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;

public class HttpClient {

    private static final String BASE_URL = "https://www.wanandroid.com/";
    private static final String TAG = HttpClient.class.getName();
    private static volatile HttpClient httpClient = null;
    private OkHttpClient okHttpClient;
    private List<String> cookie;
    private String userName;
    private String password;
    private String userNameEncode;
    private HttpClient(){
        okHttpClient = initOkHttpClient();
    }

    public static HttpClient getInstance() { //获取实例
        if (httpClient == null) {
            synchronized (HttpClient.class) {
                if (httpClient == null) {
                    httpClient = new HttpClient();
                }
            }
        }
        return httpClient;
    }

    private OkHttpClient initOkHttpClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(@NonNull String s) {
                Log.e(TAG, "net log->" + s);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .pingInterval(15, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .followRedirects(true)
                .build();
        return okHttpClient;
    }

    public void request(String path, String method, RequestBody body,Callback callback) {
        Request request = null;
        if (cookie ==null) {
            request = new Request.Builder()
                    .url(BASE_URL + path)
                    .method(method,body)
                    .build();
        } else {
            Request.Builder builder = new Request.Builder()
                    .url(BASE_URL + path)
                    .method(method,body);

            for(String header:cookie){
                builder.addHeader("Cookie",header);
            }
            try {
                userNameEncode = URLEncoder.encode(userName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            request = builder
                    .addHeader("token", "")
                    .addHeader("Cookie", "loginUserName=" + userNameEncode)
                    .addHeader("Cookie", "loginUserPassword=" + password)
                    .build();
        }
        okHttpClient.newCall(request).enqueue(callback);
    }

    public void setCookie(List<String> cookie){
        this.cookie = cookie;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}